package net.cyue.web.easyquery.test.spring.java8;

import javax.servlet.ServletContext;
import net.cyue.util.ResourceUtil;
import net.cyue.web.easyquery.core.EasyQueryApplication;
import net.cyue.web.easyquery.core.EasyQueryContext;
import net.cyue.web.easyquery.core.config.ConfigException;
import net.cyue.web.easyquery.core.config.json.EasyQueryJSONConfiguration;
import net.cyue.web.easyquery.core.http.data.PathInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class EasyQueryApplicationLauncher {

    private final Logger logger = LoggerFactory.getLogger(this.toString());

    @Autowired
    private EasyQueryApplication<ServletContext> app;

    @EventListener(ContextRefreshedEvent.class)
    public void start() throws ConfigException, IOException {
        this.logger.info("运行EasyQueryApplication");
        if (this.app == null) {
            this.logger.warn("未找到EasyQueryApplication实例");
            return;
        }
        EasyQueryContext<ServletContext> context = this.app.getContext();
        context.registerWebInterface(
            PathInfo
                .builder()
                .apiPath("/api/v1/userinfo")
                .addQueryParameter("id")
                .addQueryParameter("username")
                .build(),
            "select * from user where id = #{id} or username = #{username}"
        );
        context.addRequestPreHandler((request, response) -> {
            this.logger.info("全局预处理");
            return true;
        });
        context.addRequestPreHandler("/api/v1/user/list", (request, response) -> {
            this.logger.info("预处理 /api/v1/user/list");
            return true;
        });
        context.addRequestPreHandler("/api/v1/userinfo", (request, response) -> {
            this.logger.info("预处理 /api/v1/userinfo");
            return true;
        });
        context.addRequestPreHandler("/api/v1/user/{username}", (request, response) -> {
            this.logger.info("预处理 /api/v1/user/{username}");
            return false;
        });

        // this.app.runByProperties(ResourceUtil.getResourceAsStream("example.properties"));
        this.app.run(EasyQueryJSONConfiguration.fromStream(ResourceUtil.getResourceAsStream("example.json")));
    }
}
